package com.lj.controller;

import com.lj.config.UserFeign;
import com.lj.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Controller
//@DefaultProperties(defaultFallback = "fallback")
public class UserController {

    /*@Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;*/

    @Autowired
    private UserFeign userFeign;

    @GetMapping("/user/{id}")
    @ResponseBody
//    @HystrixCommand
    public User selectUser(@PathVariable("id") String id){
//        List<ServiceInstance> instances = discoveryClient.getInstances("PROVIDER-SERVER");
//        ServiceInstance instance = instances.get(0);
////        System.out.println(instance);
//        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/user/find/"+id;
//        String url = "http://PROVIDER-SERVER/user/find/"+id;
////        System.out.println(url);
//        return restTemplate.getForObject(url,User.class);
        return userFeign.selectUser(id);
    }


    public User fallback(){
        User user = new User("500","访问失败,请稍后再试","","","");
        return user;
    }

    public User fallback(String id){
        User user = new User("500","服务器繁忙,请稍后再试","","","");
        return user;
    }

}
